package kwic.architecture;

import java.io.*;
import java.util.*;

/**
 * 管道-过滤器体系结构实现KWIC。
 */
public class PipeFilter {
    public static void main(String[] args) throws IOException {
        File inFile = new File("src/main/resources/input.txt");
        File outFile = new File("src/main/resources/output4.txt");
        Pipe pipe1 = new Pipe();
        Pipe pipe2 = new Pipe();
        Pipe pipe3 = new Pipe();

        InputFilter input = new InputFilter(inFile, pipe1);
        ShiftFilter shift = new ShiftFilter(pipe1, pipe2);
        AlphabetizerFilter alphabetizer = new AlphabetizerFilter(pipe2, pipe3);
        OutputFilter output = new OutputFilter(outFile, pipe3);

        input.transform();
        shift.transform();
        alphabetizer.transform();
        output.transform();
    }
}

class Pipe {
    private Scanner in;
    private PrintWriter out;

    public Pipe() {
        PipedWriter pipedWriter = new PipedWriter();
        try {
            PipedReader pipedReader = new PipedReader(pipedWriter);
            in = new Scanner(pipedReader);
            out = new PrintWriter(pipedWriter);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String readLine() {
        return in.nextLine();
    }

    public boolean hasNextLine() {
        return in.hasNextLine();
    }

    public void writeLine(String line) {
        out.println(line);
    }

    public void closeReader() {
        in.close();
    }

    public void closeWriter() {
        out.close();
    }
}

abstract class Filter {
    protected Pipe input;
    protected Pipe output;

    public Filter(Pipe input, Pipe output) {
        this.input = input;
        this.output = output;
    }

    protected abstract void transform() throws IOException;
}

class InputFilter extends Filter {
    private File file;

    public InputFilter(File file, Pipe output) {
        super(null, output);
        this.file = file;
    }

    @Override
    protected void transform() throws IOException {
        try (BufferedReader inputFile = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = inputFile.readLine()) != null) {
                output.writeLine(line);
            }
            output.closeWriter();
        }
    }
}

class ShiftFilter extends Filter {
    public ShiftFilter(Pipe input, Pipe output) {
        super(input, output);
    }

    @Override
    protected void transform() throws IOException {
        while (input.hasNextLine()) {
            StringTokenizer token = new StringTokenizer(input.readLine());
            ArrayList<String> tokens = new ArrayList<>();
            while (token.hasMoreTokens()) {
                tokens.add(token.nextToken());
            }
            for (int i = 0; i < tokens.size(); i++) {
                String shiftedLine = String.join(" ", tokens);
                output.writeLine(shiftedLine);
                Collections.rotate(tokens, -1);
            }
        }
        input.closeReader();
        output.closeWriter();
    }
}

class AlphabetizerFilter extends Filter {
    private ArrayList<String> lines = new ArrayList<>();

    public AlphabetizerFilter(Pipe input, Pipe output) {
        super(input, output);
    }

    @Override
    protected void transform() throws IOException {
        while (input.hasNextLine()) {
            lines.add(input.readLine());
        }
        lines.sort(String::compareToIgnoreCase);
        for (String line : lines) {
            output.writeLine(line);
        }
        input.closeReader();
        output.closeWriter();
    }
}

class OutputFilter extends Filter {
    private File file;

    public OutputFilter(File file, Pipe input) {
        super(input, null);
        this.file = file;
    }

    @Override
    protected void transform() throws IOException {
        try (BufferedWriter outputFile = new BufferedWriter(new FileWriter(file))) {
            while (input.hasNextLine()) {
                outputFile.write(input.readLine() + "\n");
            }
            input.closeReader();
        }
    }
}
